package com.cxp.tour.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxp.tour.common.QueryPageParam;
import com.cxp.tour.common.Result;
import com.cxp.tour.entity.Scenic;
import com.cxp.tour.entity.ScenicLine;
import com.cxp.tour.service.ScenicLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tour
 * @since 2024-12-14
 */
@RestController
@RequestMapping("/scenic-line")
public class ScenicLineController {

    @Autowired
    private ScenicLineService scenicLineService;

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody ScenicLine scenicLine){
        return scenicLineService.save(scenicLine)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody ScenicLine scenicLine){
        return scenicLineService.updateById(scenicLine)?Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return scenicLineService.removeById(id)?Result.suc():Result.fail();
    }

    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody ScenicLine scenicLine){
        return scenicLineService.updateById(scenicLine);
    }

    //后端封装数据(查询)
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String detail = (String)param.get("detail");

        Page<ScenicLine> page = new Page();
        page.setCurrent(query.getPageNum());     //当前页
        page.setSize(query.getPageSize());       //当前页有多少条记录

        LambdaQueryWrapper<ScenicLine> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(detail) && !"null".equals(detail)){
            lambdaQueryWrapper.like(ScenicLine::getDetail,detail);
        }

        IPage result = scenicLineService.page(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }

}
